package com.everspring.util;

import cn.hutool.core.util.StrUtil;
import com.lowagie.text.pdf.BaseFont;
import lombok.extern.slf4j.Slf4j;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.*;

/**
 * @desc: 静态文件转pdf
 * @author: EverSpring
 */
@Slf4j
public class PdfTextUtils extends PdfWrapper{

    /**
     * 静态文件转pdf
     * @param file 文件
     * @return
     * @throws IOException
     */
    public static byte[] export(File file) throws IOException {
        if (!file.exists()) {
            throw new FileNotFoundException(file.getAbsolutePath() + "不存在");
        }
        try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            generatePdf(file, out);
            return out.toByteArray();
        } catch (IOException e) {
            log.error("export static file to pdf error");
            throw e;
        }
    }

    /**
     * 静态文件转pdf
     * @param content 要转换的内容
     * @return
     */
    public static byte[] export(String content) throws IOException {
        try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            generatePdf(content, out);
            return out.toByteArray();
        } catch (IOException e) {
            log.error("export static file to pdf error");
            throw e;
        }
    }

    /**
     * 导出pdf到指定路径
     * @param file 需要转换的文件内容
     * @param outPath 导出路径
     */
    public static void export(File file,String outPath) throws IOException{
        try(OutputStream out = new FileOutputStream(outPath)) {
            generatePdf(file, out);
            out.flush();
        } catch (IOException e) {
            log.error("export static file to pdf error");
            throw e;
        }
    }

    /**
     * 导出pdf到指定路径
     * @param content 需要转换的内容
     * @param outPath 导出路径
     */
    public static void export(String content,String outPath) throws IOException{
        try(OutputStream out = new FileOutputStream(outPath)) {
            generatePdf(content, out);
            out.flush();
        } catch (IOException e) {
            log.error("export static file to pdf error");
            throw e;
        }
    }

    /**
     * 生成pdf主逻辑
     * @param file
     * @param out
     * @throws IOException
     */
    private static void generatePdf(File file, OutputStream out) throws IOException {
        ITextRenderer renderer = new ITextRenderer();
        try {
            renderer.setDocument(file);
            dealRenderer(out, renderer);
        } finally {
            renderer.finishPDF();
        }
    }

    /**
     * 生成pdf主逻辑
     * @param content
     * @param out
     * @throws IOException
     */
    private static void generatePdf(String content, OutputStream out) throws IOException {
        ITextRenderer renderer = new ITextRenderer();
        try {
            renderer.setDocumentFromString(content);
            dealRenderer(out, renderer);
        } finally {
            renderer.finishPDF();
        }
    }

    private static void dealRenderer(OutputStream out, ITextRenderer renderer) throws IOException {
        // 解决中文支持问题
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont(getFontPath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        // 解决图片的相对路径问题
        String baseImgDir = getBaseImgDir();
        if (!StrUtil.isBlank(baseImgDir)) {
            renderer.getSharedContext().setBaseURL(baseImgDir);
        }
        renderer.layout();
        renderer.createPDF(out);
    }
}
